<?php

const SUCCESS = 0;
const ERR_UNKNOW = 1;
const ERR_PARAM = 2;
const ERR_DB = 3;
const ERR_AUTH = 100;

if(!function_exists('getPlatform'))
{
    function getPlatform($user_agent)
    {
        preg_match('/(\bios\b)|(\bandroid\b)/i',$user_agent,$matches);
        //file_put_contents('debug.log',$user_agent);
        if(strtolower(array_get($matches,1)) == 'ios')    return 'ios';
        if(strtolower(array_get($matches,2)) == 'android')    return 'android';

        return '';
    }
}


if(!function_exists('getError'))
{
    function getError($code,$desc = '', $data = null)
    {
        $errors = [
            '0'=>['ecode'=>'SUCCESS','msg'=>'success'],//成功
            '1'=>['ecode'=>'ERR_UNKNOW','msg'=>'error'],//未知错误
            '2'=>['ecode'=>'ERR_DB','msg'=>'param error'],//参数错误
            '3'=>['ecode'=>'ERR_PARAM','msg'=>'db error'],//参数错误

            '100'=>['ecode'=>'ERR_AUTH','msg'=>'unauthorized error'],//Unauthorized
        ];
        $ret = ['code'=>$code,'message'=>$errors[$code]['msg']];

        if($desc)   $ret['desc'] = $desc;
        if($code == SUCCESS)    $ret['data'] = $data;

        return $ret;
    }
}

if(!function_exists('lbResponse'))
{
    function lbResponse($data, $error)
    {
        if($data === false) return getError($error['errcode'],$error['errmsg']);
        return getError(SUCCESS,$error['errmsg'],$data);
    }
}

